import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

// 消息弹框
import 'package:fluttertoast/fluttertoast.dart';

import '../../widget/MyTextField.dart';
import '../../services/ScreenAdapter.dart';
import '../../widget/MyButton.dart';
import '../../config/Config.dart';
import '../../services/UserInfo.dart';
import '../tabs/BottomTabs.dart';

class RegisterThirdPage extends StatefulWidget {
  final Map arguments;

  RegisterThirdPage({this.arguments});

  @override
  State<StatefulWidget> createState() => RegisterThirdState(
      tel: this.arguments['tel'], code: this.arguments['code']);
}

class RegisterThirdState extends State {
  String tel;
  String code;
  String password='';
  String rPassword=''; // 确认密码
  bool obscureText = true; // 输入密码是否可见, true为默认不可见
  RegisterThirdState({this.tel, this.code});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('手机快速注册-第三步')),
        body: Container(
          padding: EdgeInsets.all(20),
          child: ListView(
            children: [
              Text('请设置登录密码'),
              SizedBox(height: 20),
              MyTextField(
                text: '请设置为6-20位字符',
                obscureText: this.obscureText, // 是否可见
                cb: (value) {
                  this.setState(() {
                    password = value;
                  });
                },
              ),
              SizedBox(height: 20),
              MyTextField(
                text: '确认密码',
                obscureText: this.obscureText, // 是否可见
                cb: (value) {
                  this.setState(() {
                    rPassword = value;
                  });
                },
              ),
              SizedBox(height: 20),
              Row(
                children: [
                  Container(
                    margin: EdgeInsets.only(right: 10),
                    decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: obscureText ? Colors.white : Colors.red),
                    child: InkWell(
                      child: Padding(
                        padding: EdgeInsets.all(2),
                        child: this.obscureText
                            ? Container(
                                width: 20,
                                child: Icon(
                                  Icons.check_box_outline_blank,
                                  size: 20.0,
                                  color: Colors.black38,
                                ),
                              )
                            : Container(
                                width: 20,
                                child: Icon(
                                  Icons.check,
                                  size: 14.0,
                                  color: Colors.white,
                                ),
                              ),
                      ),
                      onTap: () {
                        this.setState(() {
                          obscureText = !obscureText;
                        });
                      },
                    ),
                  ),
                  Text('密码可见')
                ],
              ),
              SizedBox(height: 20),
              Text('密码由6-20位字母、数组或半角符号组成，不能是10位以下纯数字/字母/半角符号，字母需区分大小写',
                  style: TextStyle(fontSize: 12, color: Colors.black38)),
              SizedBox(height: 20),
              MyButton(
                text: '完成',
                color: Colors.black12,
                textColor: Colors.black38,
                height: ScreenAdapter.height(160),
                borderRadius: 0,
                fontSize: 14,
                rightMargin: 0,
                leftMargin: 0,
                cb: _sendPassword,
              ),
              SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Text('遇到问题？您可以', style: TextStyle(color: Colors.black54)),
                  Text('联系客服',
                      style: TextStyle(
                        color: Colors.black87,
                        fontSize: 12,
                        decoration: TextDecoration.underline,
                      ))
                ],
              ),
            ],
          ),
        ));
  }

  _sendPassword() async {
    if(this.password.length >=6 ) {
      if (rPassword == password) {
        var api = Config.domain + 'api/register';
        var response = await Dio().post(api, data: {
          "tel": this.tel,
          "password": this.password,
          "code": this.code,
        });

        var res = response.data;
        print(res);
        if (res['success']) {
          // 弹框提示
          Fluttertoast.showToast(
            msg: "${res['message']}",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.CENTER,
          );

          // 将用户注册信息保存到内存中
          await UserInfoServices.setUserInfo(res['userinfo']);

          // 销毁当前页面，并返回到根
          Navigator.of(context).pushAndRemoveUntil(
              new MaterialPageRoute(builder: (context) => new BottomTabs(index: 3)),
                  (route) => route == null
          );
        } else {
          // 弹框提示
          Fluttertoast.showToast(
            msg: "${res['message']}",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.CENTER,
          );
        }
      }else {
        // 弹框提示
        Fluttertoast.showToast(
          msg: "两次输入的密码不一致，请重新输入",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
        );
      }
    } else {
      // 弹框提示
      Fluttertoast.showToast(
        msg: "密码不能小于6位",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
      );
    }
  }
}
